package com.wss.common.widget;

import butterknife.BindComponent;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.wss.common.base.ResourceTable;
import com.wss.common.utils.ImageUtils;
import com.wss.common.utils.PxUtils;
import com.wss.common.widget.attrs.StyleMultipleItemView;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import org.jetbrains.annotations.NotNull;
import java.io.IOException;

public class MultipleItemView extends DirectionalLayout implements Component.ClickedListener {

//    @BindComponent(ResourceTable.Id_rl_layout)
    DependentLayout rlLayout;

//    @BindComponent(ResourceTable.Id_iv_left)
    Image ivLeft;

//    @BindComponent(ResourceTable.Id_tv_left)
    Text tvLeft;

//    @BindComponent(ResourceTable.Id_iv_right)
    Image ivRight;

//    @BindComponent(ResourceTable.Id_tv_right)
    Text tvRight;

//    @BindComponent(ResourceTable.Id_edt_text)
    TextField edtText;

//    @BindComponent(ResourceTable.Id_ll_right)
    DirectionalLayout layout;

//    @BindComponent(ResourceTable.Id_rl_content)
    DirectionalLayout content;

//    @BindComponent(ResourceTable.Id_line_top)
    Component topLine;

//    @BindComponent(ResourceTable.Id_line_bottom)
    Component bottomLine;

    private Color leftTextColor = Color.BLACK;
    private Color rightTextColor = Color.GRAY;
    private Element leftIconResource;
    private Element rightIconResource;
    private String leftText = "";
    private String rightText = "";
    private boolean showTopLine = true;
    private boolean showBottomLine = true;
    private boolean showLeftIcon = true;
    private boolean showRightIcon = true;
    private boolean showEditText = false;

    private OnIconCheckChangedListener leftIconChangedListener, rightIconChangedListener;

    public MultipleItemView(Context context) {
        this(context, null);
    }

    public MultipleItemView(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public MultipleItemView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);

        Component layout = LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_layout_multiple_item, this, true);
        ButterKnife.bind(this, layout);

        leftTextColor = attrSet.getAttr(StyleMultipleItemView.leftTextColor).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.leftTextColor)
                        .get()
                        .getColorValue() : leftTextColor;
        rightTextColor = attrSet.getAttr(StyleMultipleItemView.rightTextColor).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.rightTextColor)
                        .get()
                        .getColorValue() : rightTextColor;

        if (attrSet.getAttr(StyleMultipleItemView.leftIcon).isPresent()) {
            leftIconResource = attrSet.getAttr(StyleMultipleItemView.leftIcon).get().getElement();
        }

        if (attrSet.getAttr(StyleMultipleItemView.rightIcon).isPresent()) {
            rightIconResource = attrSet.getAttr(StyleMultipleItemView.rightIcon).get().getElement();
        }

        leftText = attrSet.getAttr(StyleMultipleItemView.leftText).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.leftText).get().getStringValue() : leftText;
        rightText = attrSet.getAttr(StyleMultipleItemView.rightText).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.rightText).get().getStringValue() : rightText;
        showTopLine = attrSet.getAttr(StyleMultipleItemView.showTopLine).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.showTopLine)
                        .get()
                        .getBoolValue() : showTopLine;
        showBottomLine = attrSet.getAttr(StyleMultipleItemView.showBottomLine).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.showBottomLine)
                        .get()
                        .getBoolValue() : showBottomLine;
        showLeftIcon = attrSet.getAttr(StyleMultipleItemView.showLeftIcon).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.showLeftIcon)
                        .get()
                        .getBoolValue() : showLeftIcon;
        showRightIcon = attrSet.getAttr(StyleMultipleItemView.showRightIcon).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.showRightIcon)
                        .get()
                        .getBoolValue() : showRightIcon;
        showEditText = attrSet.getAttr(StyleMultipleItemView.showEdtText).isPresent() ?
                attrSet.getAttr(StyleMultipleItemView.showEdtText)
                        .get()
                        .getBoolValue() : showEditText;

        findView();

        init();
    }

    private void findView() {
        rlLayout = (DependentLayout) findComponentById(ResourceTable.Id_rl_layout);
        ivLeft = (Image) findComponentById(ResourceTable.Id_iv_left);
        tvLeft = (Text) findComponentById(ResourceTable.Id_tv_left);
        ivRight = (Image) findComponentById(ResourceTable.Id_iv_right);
        tvRight = (Text) findComponentById(ResourceTable.Id_tv_right);
        edtText = (TextField) findComponentById(ResourceTable.Id_edt_text);
        layout = (DirectionalLayout) findComponentById(ResourceTable.Id_ll_right);
        content = (DirectionalLayout) findComponentById(ResourceTable.Id_rl_content);
        topLine = findComponentById(ResourceTable.Id_line_top);
        bottomLine = findComponentById(ResourceTable.Id_line_bottom);

        tvLeft.setClickedListener(this);
        tvRight.setClickedListener(this);
        findComponentById(ResourceTable.Id_iv_right_layout).setClickedListener(this);
//        layout.setClickedListener(this);
    }

    private void init() {

        setLeftIconResource(leftIconResource);
        setRightIconResource(rightIconResource);
        setLeftText(leftText);
        setRightText(rightText);
        setShowLeftIcon(showLeftIcon);
        setShowRightIcon(showRightIcon);
        setShowTopLine(showTopLine);
        setShowBottomLine(showBottomLine);
        setShowEditText(showEditText);
        tvLeft.setTextColor(leftTextColor);
        edtText.setTextColor(rightTextColor);
        tvRight.setTextColor(rightTextColor);

        //如果手动设置了背景 则赋值给layout 否则设置默认颜色
        Element background = getBackgroundElement();
        if (background != null) {
            rlLayout.setBackground(background);
        } else {
            ShapeElement shapeElement = new ShapeElement();
            shapeElement.setRgbColor(new RgbColor(Color.WHITE.getValue()));
            rlLayout.setBackground(shapeElement);
        }
    }

    /**
     * 设置是否显示EditText
     *
     * @param show text
     * @return MultifunctionalItemView
     */
    public MultipleItemView setShowEditText(boolean show) {
        if (show) {
            edtText.setVisibility(VISIBLE);
            tvRight.setVisibility(HIDE);
        }
        return this;
    }

    /**
     * 设置上面线是否显示
     *
     * @param show text
     * @return MultifunctionalItemView
     */
    public MultipleItemView setShowTopLine(boolean show) {
        topLine.setVisibility(show ? VISIBLE : HIDE);
        return this;
    }

    /**
     * 设置下面线是否显示
     *
     * @param show text
     * @return MultifunctionalItemView
     */
    public MultipleItemView setShowBottomLine(boolean show) {
        bottomLine.setVisibility(show ? VISIBLE : HIDE);
        return this;
    }

    /**
     * 设置左边文字
     *
     * @param text text
     * @return MultifunctionalItemView
     */
    public MultipleItemView setLeftText(String text) {
        tvLeft.setText(text);
        return this;
    }

    /**
     * 设置左边文字颜色
     *
     * @param color 颜色
     * @return MultifunctionalItemView
     */
    public MultipleItemView setLeftTextColorResource(int color) {
        try {
            tvLeft.setTextColor(new Color(
                    getResourceManager().getElement(color).getColor()
            ));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 设置左边文字大小
     *
     * @param size 大小
     * @return MultifunctionalItemView
     */
    public MultipleItemView setLeftTextSize(int size) {
        tvLeft.setTextSize(size);
        return this;
    }

    /**
     * 设置右边文字
     *
     * @param text text
     * @return MultifunctionalItemView
     */
    public MultipleItemView setRightText(String text) {
        tvRight.setText(text);
        edtText.setText(text);
        return this;
    }

    /**
     * 设置右边文字颜色
     *
     * @param color 颜色
     * @return MultifunctionalItemView
     */
    public MultipleItemView setRightTextColorResource(int color) {
        try {
            int resColor = getResourceManager().getElement(color).getColor();
            tvRight.setTextColor(new Color(resColor));
            edtText.setTextColor(new Color(resColor));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NotExistException e) {
            e.printStackTrace();
        } catch (WrongTypeException e) {
            e.printStackTrace();
        }

        return this;
    }

    /**
     * 设置右边文字大小
     *
     * @param size 大小
     * @return MultifunctionalItemView
     */
    public MultipleItemView setRightTextSize(int size) {
        tvRight.setTextSize(PxUtils.fp2px(size));
        edtText.setScaleX(size);
        return this;
    }

    /**
     * 设置左边Icon Margin
     *
     * @param left   左
     * @param top    上
     * @param right  右
     * @param bottom 下
     * @return MultifunctionalItemView
     */
    public MultipleItemView setLeftIconMargin(int left, int top, int right, int bottom) {
        DirectionalLayout.LayoutConfig lc = (LayoutConfig) ivLeft.getLayoutConfig();
        lc.setMargins(
                PxUtils.vp2px(left),
                PxUtils.vp2px(top),
                PxUtils.vp2px(right),
                PxUtils.vp2px(bottom)
        );
        return this;
    }

    /**
     * 设置右边Icon Margin
     *
     * @param left   左
     * @param top    上
     * @param right  右
     * @param bottom 下
     * @return MultifunctionalItemView
     */
    public MultipleItemView setRightIconMargin(int left, int top, int right, int bottom) {

        DirectionalLayout.LayoutConfig lc = (LayoutConfig) ivRight.getLayoutConfig();
        lc.setMargins(
                PxUtils.vp2px(left),
                PxUtils.vp2px(top),
                PxUtils.vp2px(right),
                PxUtils.vp2px(bottom)
        );

        return this;
    }


    /**
     * 是否显示左边图标
     *
     * @param show show
     * @return MultifunctionalItemView
     */
    public MultipleItemView setShowLeftIcon(boolean show) {
        ivLeft.setVisibility(show ? VISIBLE : HIDE);
        return this;
    }

    /**
     * 设置左边图标图片
     *
     * @param res 资源
     * @return MultifunctionalItemView
     */
    public MultipleItemView setLeftIconResource(Element res) {

        if (res == null) {
            try {
                PixelMapElement element = new PixelMapElement(getContext()
                        .getResourceManager()
                        .getResource(ResourceTable.Media_ic_launcher));
                this.ivLeft.setBackground(element);

            } catch (IOException e) {
                e.printStackTrace();
            } catch (NotExistException e) {
                e.printStackTrace();
            }
        } else {
            this.ivLeft.setBackground(res);
        }

        return this;
    }

    /**
     * 设置左边图标大小
     *
     * @param width  宽
     * @param height 高
     * @return MultifunctionalItemView
     */
    public MultipleItemView setLeftIconSize(int width, int height) {

        DirectionalLayout.LayoutConfig lc = new LayoutConfig(PxUtils.vp2px(width),
                PxUtils.vp2px(height));
        lc.setMargins(0, 0, 4, 0);
        ivLeft.setLayoutConfig(lc);

        return this;
    }


    /**
     * 是否显示右边图标
     *
     * @param show show
     * @return MultifunctionalItemView
     */
    public MultipleItemView setShowRightIcon(boolean show) {
        ivRight.setVisibility(show ? VISIBLE : HIDE);
        return this;
    }

    /**
     * 设置右边图标资源
     *
     * @param res 资源
     * @return MultifunctionalItemView
     */
    public MultipleItemView setRightIconResource(Element res) {

        if (res == null) {
            this.ivRight.setPixelMap(ResourceTable.Media_icon_more);
        } else {
            this.ivRight.setPixelMap(((PixelMapElement)res).getPixelMap());
        }
        return this;
    }

    public MultipleItemView setRightIconResource(int resId) {
        this.ivRight.setPixelMap(resId);
        return this;
    }

    /**
     * 设置右边图标大小
     *
     * @param width  宽
     * @param height 高
     * @return MultifunctionalItemView
     */
    public MultipleItemView setRightIconSize(int width, int height) {
        ivRight.setLayoutConfig(new ComponentContainer.LayoutConfig(
                PxUtils.vp2px(width),
                PxUtils.vp2px(height)
        ));
        return this;
    }

    /**
     * 设置父窗体 padding
     *
     * @param left   左
     * @param top    上
     * @param right  右
     * @param bottom 下
     * @return MultifunctionalItemView
     */
    public MultipleItemView setContentPadding(int left, int top, int right, int bottom) {
        content.setPadding(PxUtils.vp2px(left), PxUtils.vp2px(top),
                PxUtils.vp2px(right), PxUtils.vp2px(bottom));
        return this;
    }

    /**
     * 设置右icon选择
     *
     * @param checked 选择
     * @return this
     */
    public MultipleItemView setRightIconChecked(boolean checked) {
        ivRight.setSelected(checked);
        if (leftIconChangedListener != null) {
            leftIconChangedListener.onIconChanged(checked);
        }
        return this;
    }

    /**
     * 设置左icon选择
     *
     * @param checked 选择
     * @return this
     */
    public MultipleItemView setLeftIconChecked(boolean checked) {
        ivRight.setSelected(checked);
        if (rightIconChangedListener != null) {
            rightIconChangedListener.onIconChanged(checked);
        }
        return this;
    }

    /**
     * 左侧按钮变化监听
     *
     * @param iconChangedListener 监听器
     * @return this
     */
    public MultipleItemView setLeftIconChangedListener(OnIconCheckChangedListener iconChangedListener) {
        this.leftIconChangedListener = iconChangedListener;
        return this;
    }

    /**
     * 右侧按钮变化监听
     *
     * @param iconChangedListener 监听器
     * @return this
     */
    public MultipleItemView setRightIconChangedListener(OnIconCheckChangedListener iconChangedListener) {
        this.rightIconChangedListener = iconChangedListener;
        return this;
    }

    /**
     * 设置父窗体 padding
     *
     * @param padding padding
     * @return MultifunctionalItemView
     */
    public MultipleItemView setContentPadding(int padding) {
        return setContentPadding(padding, padding, padding, padding);
    }

    /**
     * 设置右边View
     *
     * @param view view
     * @return MultifunctionalItemView
     */
    public MultipleItemView setRightView(Component view) {
        this.layout.removeAllComponents();
        this.layout.addComponent(view);
        return this;
    }

    /**
     * 当在输入模式下 返回输入框的值
     *
     * @return String
     */
    public String getRightTex() {
        return edtText.getText().toString().trim();
    }


    @Override
    public void onClick(Component view) {

        view.setSelected(!view.isSelected());
        if (view.getId() == ResourceTable.Id_iv_left) {
            if (leftIconChangedListener != null) {
                leftIconChangedListener.onIconChanged(view.isSelected());
            }
        } else if (view.getId() == ResourceTable.Id_iv_right
                || view.getId() == ResourceTable.Id_iv_right_layout) {

            if (rightIconChangedListener != null) {
                rightIconChangedListener.onIconChanged(view.isSelected());
            }
        }
    }

    /**
     * 左右按钮 选择状态变化
     */
    public interface OnIconCheckChangedListener {
        /**
         * 按钮选择状态变化
         *
         * @param checked 选择状态
         */
        void onIconChanged(boolean checked);
    }
}
